<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
   <% String path=request.getContextPath(); 
   String basePath=request.getScheme()
    + "://" + request.getServerName() + ":" + request.getServerPort() + path
    + "/"; 

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<base href="<%=basePath%>">
<title>角色维护</title>
<link rel="stylesheet" href="<%=path%>/css/zTreeStyle/zTreeStyle.css">
<jsp:include page="/WEB-INF/jsp/common/common.jsp"></jsp:include>
<script type="text/javascript" src="<%=path%>/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="<%=path%>/js/jquery.ztree.excheck-3.5.js"></script>
<script src="<%=path%>/js/jquery.divbox.js" type="text/javascript"></script>
<script>
var BRANCHCODE = ${BRANCHCODE};
$(document).ready(function() {
	isReadyIframe();
	queryRole(1);
});

function isReadyIframe(){
	var iframe = $(window.parent.document).find("#iframe").length;
	if(iframe==0){
		window.location.href="<%=path%>/index.jsp";
	}
}

//关闭  窗口
function closeCurrentDIV(obj){
	if(obj == 'deptInfoDiv'){
		$("#"+obj).CloseDiv2();
		}else{
		$("#"+obj).CloseDiv();	
	}
}


/**
 * 页面初始化查询显示所有角色列表
 */
function queryRole(pageIndex){
	var rname = $('#roleName_input').val();
	$.openLoading();
	$.ajax({
		url : "<%=path%>/systemManager/roleManagement/initAllRoles",
		type : 'POST',
		async : true,
		data:{"pageIndex":pageIndex,"roleName":rname},
		dataType:'json',
		success:function(rolePage){
			$.closeLoading();
			if($.validate(rolePage))return;
			//业务处理
				if(null != rolePage){
					var buttonStr = "<a href='javascript:void(0)' onClick='editRole(this);return false'>编辑</a>&nbsp;<a href='javascript:void(0)' onClick='delRole(this)'>删除</a>&nbsp;<a href='javascript:void(0)' onClick='openAllMenuDiv(this)'>配置菜单</a>";
					var fieldNames = "roleid@TDH,role_name,user_name,oper_time,DEPT_CODE@TDH,DEPT_CNAME";
					var tableContent = creatTable(fieldNames,rolePage,buttonStr);
					var tableObj = $("#roleTable");
					tableObj.find("tbody").remove();
					tableObj.append(tableContent);

					var strPage = creatChannelPage(pageIndex,6,rolePage.maxPage,"8");

					var pageDIV = $("#rolePageDIV");
					pageDIV[0].innerHTML="";
					pageDIV.append(strPage);
				}
		}
	});
}


//删除之前 弹出提示框
function warnDel(roleid,data){
	$.dialog({
		type: "warn",
		content: "确定要删除角色吗？目前系统中有"+data+"个用户配置有此角色",
		onOk: function() {
			excuteDel(roleid);
		}
	});
}


//删除某角色之前判断已有多少个用户配置有该角色
function delRole(obj){
	var delTr = $(obj).parent('td').parent('tr')[0];
	var roleid = delTr.childNodes[0].innerHTML;
	$.ajax({
		url : "<%=path%>/systemManager/roleManagement/getUserCountOfOneRole",
		type : 'POST',
		async : true,
		data:{"roleid":roleid},
		dataType:'json',
		success:function(data){
				warnDel(roleid,data);
		}
	});
}

//删除角色，包括角色菜单关联、用户角色关联
function excuteDel(roleid){
	$.ajax({
		url : "<%=path%>/systemManager/roleManagement/delRole",
		type : 'POST',
		async : true,
		data:{"roleid":roleid},
		dataType:'json',
		success:function(data){

			
			if($.validate(data))return;
			
				//删除成功后重新加载页面
				queryRole(1);
			}
	});
}
//打开添加角色窗口
function openAddRoleDIV(){
	$('#deptCode').val('');
	$('#roleName').val('');
	$("#roleDiv").OpenDiv();
	$("#saveRoleBtn").attr("onclick","").unbind("click").click(function(){  
		addRole();
	}) 	
}

//添加角色
function addRole(){
	var roleName = $.trim($('#roleName').val());
	var deptCode = $.trim($('#deptCode').val());
	if(roleName.length==0){
		$.message("error","请输入角色名称");
		return;
	}
	if($.trim($('#deptCode').val()).length==0){
		$.message("error","请选择配置机构");
		return;
	}
	$.ajax({
		url : "<%=path%>/systemManager/roleManagement/addRole",
		type : 'POST',
		async : true,
		data:{"roleName":roleName,"deptCode":deptCode},
		dataType:'json',
		success:function(data){
			if(data!=null && data.result == "error"){
				$.message("error", data.message);
			}else{
				$.message("success", data.message);
				//添加成功后重新加载页面
				closeCurrentDIV('roleDiv');
				queryRole(1);
			}
		}
	});
}


var rid;
var oriname;
//编辑角色
function editRole(obj){
	var delTr = $(obj).parent('td').parent('tr')[0];
	var roleName = delTr.childNodes[1].innerHTML;
	var deptCode = delTr.childNodes[4].innerHTML;
	rid = delTr.childNodes[0].innerHTML;
	$('#roleName').val(roleName);
	$('#deptCode').val(deptCode);
	$("#roleDiv").OpenDiv();
	$("#saveRoleBtn").attr("onclick","").unbind("click").click(function(){  
		saveRole();  
		}) 	
	oriname = $('#roleName').val();
}

//保存修改角色
function saveRole(){
	var lastInputName = $('#roleName').val();
	var deptCode = $('#deptCode').val();

	if($.trim($('#roleName').val()).length==0){
		$.message("error","请输入角色名称");
		return;
	}
	if($.trim($('#deptCode').val()).length==0){
		$.message("error","请选择配置机构");
		return;
	}
	/* //alert("oriname---------"+oriname+"----------lastInputName--------"+lastInputName);
	if(oriname==lastInputName){
		$.message("error","您未做任何修改");
		return;
	} */
	
	$.ajax({
		url : "<%=path%>/systemManager/roleManagement/modifyRole",
		type : 'POST',
		async : true,
		data:{"roleName":lastInputName,"roleid":rid,"deptCode":deptCode},
		dataType:'json',
		success:function(data){
			if(data!=null && data.result == "error"){
				$.message("error", data.message);
			}else{
				$.message("success", data.message);
				//修改保存成功后重新加载页面
				closeCurrentDIV('roleDiv');
				queryRole(1);
			}
		}
	});
}







//获取选中的菜单
	var idstr = '';
function chooseMenus(e, treeId, treeNode) {
	var sNodes = zTreeObj.getCheckedNodes();
	if(sNodes.length>0){
		for(var i=0;i<sNodes.length;i++){
			idstr+= sNodes[i].id+',';
		}
	}
}	

var roleid ;
//打开菜单窗口
function openAllMenuDiv(obj){
	var delTr = $(obj).parent('td').parent('tr')[0];
	roleid = delTr.childNodes[0].innerHTML;
	var deptCode = delTr.childNodes[4].innerHTML;
	if(!BRANCHCODE){
		if(deptCode.length<=2){
			$.message("error","您无权操作分公司级别角色的菜单");
			return;
		}
	}
	menuTree();
	$("#pMenuDiv").OpenDiv();
}
//初始化树形菜单
function menuTree(){
	var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			view: {
				dblClickExpand: true
			},
			callback: {
				onClick: openTree
			},
			check: {
				enable: true,
				chkStyle: "checkbox",
				chkboxType: { "Y": "p", "N": "s" }
			}
		};
	$.ajax({
		url: "<%=path%>/systemManager/menuManage/selectMenusToRole",
		type: "post",
		dataType: "json",
		async:true,
		cache:false,
		success: function(data) {

			if($.validate(data))return;
			//业务处理
				zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, data);

				$.ajax({
					url: "<%=path%>/systemManager/roleManagement/queryAllMenusForCurRole",
					type: "post",
					dataType: "json",
					data:{"roleid":roleid},
					async:true,
					success: function(data) {
						if($.validate(data))return;
						//业务处理
							if(data!=null){
								$.each(data,function(i,item){
									zTreeObj.checkNode(zTreeObj.getNodeByParam("id", item.menuid, null), true, true);
								})
							}else{
								$.message("error", "菜单加载失败");
							}
					}
				});
		}
	});
}


//保存菜单配置
function saveMenu(){
	idstr='';
	chooseMenus();
	if(idstr==''){
		$.message("error","请选择一个菜单");
		return;
	}
	$.openLoading();
	$.ajax({
		url: "<%=path%>/systemManager/roleManagement/bindMenusToRole",
		type: "post",
		dataType: "json",
		data:{"menuidStr":idstr,"roleid":roleid},
		async:true,
		success: function(data) {
			$.closeLoading();
			if($.validate(data))return;
			closeCurrentDIV('pMenuDiv');
		}
	});
}

//展开树形菜单
function openTree(event, treeId, treeNode){
	if(treeNode.isParent){
		var isOpen = treeNode.open;
		zTreeObj.expandNode(treeNode,!isOpen);
	}
}

<%--加载所属权限机构  begin-------------------------------------------------%>
function openThisDiv(pageIndex,obj){
	$("#"+obj.id).blur();
	queryPrivilegeDeptInfo(pageIndex);
}
//绑定单选按钮事件，选中某个单选按钮，该按钮所在行高亮显示
function bindradio(){
	$("input[name='deptRadio']").click(function(){
		$("input[name='deptRadio']").parent().parent().removeClass("table_highlighter");
		$(this).parent().parent().addClass("table_highlighter");
   	});
}

function queryPrivilegeDeptInfo(pageIndex){
	$("#BigDiv2").remove();
	var deptCode = $.trim($("#deptCode_search").val());
	var deptCname = $.trim($("#deptName_search").val());
	$.openLoading();
	$.ajax({
		<%--url : "<%=path%>/channelManage/queryBranchDept?"+new Date().getTime(), noted--%>
		url : "<%=path%>/systemManager/roleManagement/getBranchDepts?"+new Date().getTime(),
		type : 'POST',
		async : true,
		dataType:'json',
		data:{
			deptCode:deptCode,
			deptCname:deptCname,
			pageIndex:pageIndex
		},
		success:function(page){
			$.closeLoading();
			if($.validate(page))  return;
			if(null != page){
				var radioObj ="<input class='input_fxk' name='deptRadio' type='radio' value=''>";
				var fieldNames = "dept_code,dept_cname";
				var tableContent = creatTable1(fieldNames,page,radioObj);
				var tableObj = $("#DeptInfoTable");
				tableObj.find("tbody").remove();
				tableObj.append(tableContent);
				
				var strPage = creatChannelPage2(pageIndex,6,page.maxPage,"25");
				var pageDIV = $("#deptInfoPageDIV");
				pageDIV[0].innerHTML="";
				pageDIV.append(strPage);
				
				$('#deptInfoDiv').OpenDiv2();
				
				bindradio();
			}
		}
	});
}

function chooseDept(){
	var selectedDept = $("input[name='deptRadio']:checked");
	if(selectedDept.length==0){
		$.message("error", "请选择一个机构");
		return;
	}
	var deptCode = selectedDept.parent().parent().children(" td:eq(1)").html();
	$('#deptCode').val(deptCode);
	closeCurrentDIV('deptInfoDiv');
}

<%--加载所属权限机构  end-------page----------------------%>

</script>
</head>

<body>
	<div class="current">
           <table class="current_table_1">
              <tbody><tr class="f2">
                <td><img src="./images/current_1.jpg"></td>
                  <td class="current_table_1_td">
                  <img src="images/current_1.png">&nbsp;您现在的位置：&nbsp;
                  <span style=" color:#136bab; ">系统管理
                  <span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>角色维护
                  </span>
                  </td>
                  <td><img src="./images/current_3.jpg"></td>
              </tr>
           </tbody></table>
      </div>

	<%--显示role的表格 --%>
	<table border="1" class="table_02" style="height:150px">
  		<tr height="50px">
  			<td colspan="2" align="center">
				角色名称：&nbsp;<input id="roleName_input" class="input_text"/>&nbsp;&nbsp;
				<input type="button" id="queryRoleBtn" class="input_button_1" value="查询" onclick="queryRole(1)"/>
				<input type="button" id="addRoleBtn" class="input_button_1"  value="新增" onclick="openAddRoleDIV()"/>
			</td>
  		</tr>
  		<tr>
  			<td valign="top">
	  			<div id="roleDIV" style="width:98%;margin:5px">
			  		<table class="table_02" id="roleTable">
			  		<thead>
		              <tr>
		                  <th style="width:20%">角色名称</th>
		                  <th style="width:20%">操作人</th>
		                  <th style="width:20%">操作时间</th>
		                  <th style="width:25%">所属机构</th>
		                  <th style="width:15%">操作</th>
		              </tr>
		              </thead>
		              <tbody>
					  </tbody>
		            </table>
						<div id="rolePageDIV"  class="page" >
	  				 </div>
	  			</div>
  			</td>
  		</tr>
  	</table>
  	
  	
  		<!--   	添加、修改角色DIV 默认隐藏-->
  		<div id="roleDiv" style="display:none;width:600px;">
		<input type="hidden" id="row_num" name="row_num" value="0"/>
			<div class="table_title_div">
					<table class="table_title" style='width:100%;height: 20px;'>
						<tr>
						<td width="10%"></td>
						<td width="80%" id="interfaceName"></td>
						<td width="10%" align="right"><img src="<%=path %>/images/closedown.gif" alt="关闭"  onclick="closeCurrentDIV('roleDiv')"/></td>
						</tr>
					</table>
			</div>
		<table class="table_02" id="saveAppTable" style="height:80px;width: 90%" align="center" >
			<tr>
				<td style="width:40%;text-align:right; border-right: 0px;">
					角色名称：
				</td>
				<td style="width:60%;text-align:left;">
					<input type="text" id="roleName" class="input_text"/><span class="red">*&nbsp;</span>(长度不能超过10位字符)
				</td>
			</tr>
			<tr>
				<td style="width:40%;text-align:right;border-right: 0px;">
					配置机构：
				</td>
				<td style="width:60%;text-align:left;">
					<input name="deptCode" id="deptCode" type="text" readonly="readonly" style=" background-color: #ccc;" class="input_text"><span class="red">*</span>&nbsp;
					<input type="button"  class="input_button_2" id="searchPartnerBtn" value="请选择" onClick="openThisDiv(1,this)"/>
				</td>
			</tr>
		</table>
		<div style="width: 95%;height:30px;" align="center">
		    <input type="button" id="saveRoleBtn" value="保存" onclick="addRole()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
		    <input type="button" value="取消" onclick="closeCurrentDIV('roleDiv')" class="input_button_3"/>
		</div>
	</div>
 
 <!-- 	父级菜单 树形div -->
<div id="pMenuDiv" style="border:0px solid red;display:none;text-align: center;width:260px; ">
	<div>
			<div class="table_title_div">
			<table class="table_title" style='width:100%;height: 20px;'>
						<tr>
						<td width="10%"></td>
						<td width="80%" id="interfaceName"></td>
						<td width="10%" align="right"><img src="<%=path %>/images/closedown.gif" alt="关闭"  onclick="closeCurrentDIV('pMenuDiv')"/></td>
						</tr>
					</table>
			</div>
			<div id="tt" style="border:0px solid yellow;height:300px;width:100%;overflow-y:auto;">
			<ul id="treeDemo" class="ztree" ></ul>
			</div>
			<table class="table_02" style="height:30px;width:100%;" align="center" >
		</table>
		<div  style='width:100%;'>
		     <input type="button" id="saveBtn" value="确定" onclick="saveMenu()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
		     <input type="button" value="取消" onclick="closeCurrentDIV('pMenuDiv')" class="input_button_3"/>
		</div>
	</div>
</div>

	<!--配置机构Div-->
  	<div id="deptInfoDiv" style="display:none;width:80%;">
  		<input type="hidden" id="row_num" name="row_num" value="0"/>
			<div class="table_title_div">
					<table class="table_title" style='width:100%;height: 20px;'>
						<tr>
						<td width="10%"></td>
						<td width="80%" id="interfaceName"></td>
						<td width="10%" align="right"><img src="./images/closedown.gif" alt="关闭"  onclick="closeCurrentDIV('deptInfoDiv')"/></td>
						</tr>
					</table>
			</div>
		<table class="table_02" id="table" style="height:180px;" align="center">
			<tr>
				<td>
					<table class="search_table">
						<tr height="30px">
							<td colspan="3" style="text-align:center">
							 机构代码：<input name="txt3"  id="deptCode_search"  type="text" class="input_text" value="" >&nbsp;&nbsp;
							 机构名称：<input name="txt3" id="deptName_search"  type="text" class="input_text" value="">&nbsp;&nbsp;
							<input type="button"  class="input_button_1" id="searchBtn" value="查询" onClick="queryPrivilegeDeptInfo(1)"/>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<table id="DeptInfoTable"  class="content_table_1">
						<thead>
							<tr class="f1">
			                  <th style="width:5%">选择</th>
			                  <th style="width:45%">机构代码</th>
			                  <th style="width:50%">机构名称</th>
			              	</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
					<div id="deptInfoPageDIV"  class="page"></div>
				</td>
			</tr>
		</table>
		<div align="center" >
		  <input type="button" id="saveDeptInfoBtn" value="确定" onclick="chooseDept()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
		  <input type="button" value="取消" onclick="closeCurrentDIV('deptInfoDiv')" class="input_button_3"/>
		</div>
	</div>
 
</body>
</html>
